// pages/search/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    param: '',
    history: [],
    from: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().getSaleMode()
    const { from } = options
    if (from) this.setData({ from })
  },
  updateSearchKey(e) {
    const { value } = e.detail
    this.setData({
      param: value
    })
  },
  goSearch() {
    const { param, from } = this.data
    let filters = wx.getStorageSync('filters')
    let setP = []
    if (filters) {
      setP = filters.split(',')
    }
    if (param.length > 0) setP.push(param)
    let str = [...new Set(setP)]
    wx.setStorage({
      key: 'filters',
      data: str.join(','),
    })
    wx.setStorageSync("if_refresh", 2)
    wx.setStorage({
      key: 'tagValue',
      data: param,
      success: () => {
        if (from && from * 1 === 1) {
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.setData({
            param,
            list: [],
            pageNo:1
          })
          prevPage.ItemList('Refresh')
          wx.navigateBack()
        } else {
          wx.switchTab({
            url: `../product/index/index`,
          })
        }
      }
    })
  },
  remove() {
    wx.removeStorageSync("filters")
    this.setData({
      history: []
    })
  },
  searchHandler(e) {
    console.log(123)
    debugger
    const { param } = e.currentTarget.dataset
    wx.setStorageSync('tagValue', param)
    wx.setStorageSync("if_refresh", 2)
    const { from } = this.data
    if (from && from * 1 === 1) {
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]; //上一个页面
      prevPage.setData({param, list:[], pageNo:1})
      prevPage.ItemList('Refresh')
      wx.navigateBack({})
    } else {
      wx.switchTab({
        url: `../product/index/index`,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.getStorage({
      key: 'filters',
      complete: (res) => {
        const { data } = res
        if (data) {
          this.setData({
            history: data.split(',')
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})